package com.csun.nusing.ui.activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.BaseJsonBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.ToastUtil;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密吗 ClassName: NursingRepsdActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-2
 */
public class NursingRepsdActivity extends BaseActivity<CommonPresenter> {

	private Button comfirmpsdbtn;
	private EditText oldpsdet;
	private EditText newpsdet;
	private EditText repsdet;
	private String userid;
	private String authorization;
	private String password;
	private String repassword;
	private String oldpassword;
	private String mypassword;

	@Override
	public int getContentViewId() {
		return R.layout.activity_nursing_repsd;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	/**
	 * 初始化控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-2
	 */
	private void findView() {
		oldpsdet = findViewById(R.id.nursing_repsd_old_psd_et);
		newpsdet = findViewById(R.id.nursing_repsd_new_psd_et);
		repsdet = findViewById(R.id.nursing_repsd_re_psd_et);
		comfirmpsdbtn = findViewById(R.id.nursing_repsd_psd_comfirm_btn);
		comfirmpsdbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				comfirm();
			}
		});
	}

	protected void comfirm() {
		mypassword = (String) SPUtils.get(this, "PASSWORD", "");
		password = newpsdet.getText().toString().trim();
		repassword = repsdet.getText().toString().trim();
		oldpassword = oldpsdet.getText().toString().trim();
		if (!oldpassword.equals(mypassword)) {
			ToastUtil.showMessage(this, "原密码有误");
			return;
		}
		if (password.length() < 6) {
			ToastUtil.showMessage(this, "密码长度太短");
			return;
		} else if (password.length() > 16) {
			ToastUtil.showMessage(this, "密码长度过短");
			return;
		}
		if (!repassword.equals(password)) {
			ToastUtil.showMessage(this, "两次输入密码不一致");
			return;
		}
		userid = (String) SPUtils.get(this, "userId", "");
		Map<String, String> param = new HashMap<String, String>();
		param.put("userid", userid);
		param.put("oldpassword", oldpassword);
		param.put("password", password);
		param.put("repassword", repassword);

		authorization = (String) SPUtils.get(this, "authorization", "");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							BaseJsonBean baseJsonBean = gson.fromJson(result,
									BaseJsonBean.class);
							ToastUtil.showMessage(getApplicationContext(),
									baseJsonBean.getMessage());
							if (baseJsonBean.isStatus()) {
								finish();
							}
						}
					}
				}).start(UrlBean.UPDATEPASSWORDURL);
	}

	@Override
	public void initData() {
		super.initData();
	}

}
